package com.tanhua.server.service;

import com.alibaba.fastjson.JSON;
import com.tanhua.autoconfig.template.AipFaceTemplate;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.autoconfig.template.OssTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.mongoUser;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserInfoService {

    @DubboReference
    private UserInfoApi userInfoApi;


    @Autowired
    private OssTemplate ossTemplate;

    @Autowired
    private AipFaceTemplate aipFaceTemplate;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    @DubboReference
    private UserLikeApi userLikeApi;

    @Autowired
    private MessagesService messagesService;

    public void save(UserInfo userInfo) {
        userInfoApi.save(userInfo);
    }

    //更新用户头像
    public void updateHead(MultipartFile headPhoto, Long id) throws IOException {
        //1、将图片上传到阿里云oss
        String imageUrl = ossTemplate.upload(headPhoto.getOriginalFilename(), headPhoto.getInputStream());
        //2、调用百度云判断是否包含人脸
        boolean detect = aipFaceTemplate.detect(imageUrl);
        //2.1 如果不包含人脸，抛出异常
        if(!detect) {
            throw new BusinessException(ErrorResult.faceError());
        }else{
            //2.2 包含人脸，调用API更新
            UserInfo userInfo = new UserInfo();
            userInfo.setId(id);
            userInfo.setAvatar(imageUrl);
            userInfoApi.update(userInfo);
        }
    }

    //根据id查寻
    public UserInfoVo findById(Long id) {
        UserInfo userInfo = userInfoApi.findById(id);

        UserInfoVo vo = new UserInfoVo();

        BeanUtils.copyProperties(userInfo,vo); //copy同名同类型的属性

        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }

        return vo;
    }

    //更新
    public void update(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }

    public PageResult findByType(Integer page, Integer pagesize, Integer type, String nickname) {
        List<mongoUser> mongoUserList = new ArrayList<>();
        PageResult pageResult = new PageResult();
        List<UserInfo> userInfos = new ArrayList<>();
        //互相关注
        if (type.equals(1)) {
            List<Long> Ids = userLikeApi.relativeLike(UserHolder.getUserId(), page, pagesize, type, nickname);
            userInfos = userInfoApi.findByIds(Ids);
            //我的关注
        } else if (type.equals(2)) {
            List<Long> Ids = userLikeApi.meLike(UserHolder.getUserId(), page, pagesize, type, nickname);
            userInfos = userInfoApi.findByIds(Ids);
        } else if (type.equals(3)) {
            //粉丝
            List<Long> Ids = userLikeApi.likeMeList(UserHolder.getUserId(), page, pagesize, type, nickname);
            userInfos = userInfoApi.findByIds(Ids);
        }
        if (type.equals(4)) {
            List<Long> Ids = userLikeApi.visitors(UserHolder.getUserId(), page, pagesize);
            userInfos = userInfoApi.findByIds(Ids);
            for (UserInfo userInfo : userInfos) {
                mongoUser mongoUser = JSON.parseObject(JSON.toJSONString(userInfo), mongoUser.class);
                mongoUserList.add(mongoUser);
            }
            return new PageResult(page, pagesize, 0L, mongoUserList);
        }
        //遍历集合
        for (UserInfo userInfo : userInfos) {
            mongoUser mongoUser = JSON.parseObject(JSON.toJSONString(userInfo), mongoUser.class);
            Long id = userInfo.getId();
            List<UserLike> list = userLikeApi.userLike2(id, UserHolder.getUserId());
            if (list != null) {
                mongoUser.setAlreadyLove(true);
            }
            mongoUserList.add(mongoUser);
        }
        return new PageResult(page, pagesize, 0L, mongoUserList);
    }

    public void addLike(Long likeUserId) {
        //添加到喜欢表
        userLikeApi.addLike(UserHolder.getUserId(), likeUserId);
        //判断是否是互相喜欢 互相喜欢要添加到好友表和环信好友中
        boolean b = userLikeApi.ifLike(UserHolder.getUserId(), likeUserId);
        if (b) {
            messagesService.contacts(likeUserId);
        }
    }

    public void deleLike(Long likeUserId) {
        //删除该次喜欢
        userLikeApi.deleLike(UserHolder.getUserId(), likeUserId);
        //判断是否是互相喜欢 如果互相喜欢一方删除喜欢需要删除好友关系和环信好友关系
        boolean b = userLikeApi.ifLike(UserHolder.getUserId(), likeUserId);
        if (b) {
            //删除环信好友
            huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                    Constants.HX_USER_PREFIX + likeUserId);
            huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + likeUserId,
                    Constants.HX_USER_PREFIX + UserHolder.getUserId());
            //双向删除好友数据
            userLikeApi.delefriend(UserHolder.getUserId(),likeUserId);
            userLikeApi.delefriend(likeUserId,UserHolder.getUserId());
        }
    }
}
